//
//  BaseViewController.m
//  FashionSpring
//
//  Created by 吕飞俊 on 15/7/1.
//  Copyright (c) 2015年 润媛. All rights reserved.
//

#import "BaseViewController.h"
#import "UIFont+lvfjHelper.h"

@implementation BaseViewController
/// .IOS判断当前UIViewController 是否正在显示。
- (BOOL)isVisible {
    return (self.isViewLoaded && self.view.window);
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont ChineseFontWithSize:16],NSForegroundColorAttributeName:Title_Nav_Color}];

    NSLog(@"%lu",(unsigned long)self.navigationController.viewControllers.count);
//    if (self.navigationController.viewControllers.count > 1) {
//        self.navigationItem.backBarButtonItem.tintColor = Title_Nav_Color;
//        self.navigationController.navigationBar.tintColor = Title_Nav_Color;
//        [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0)
//                                                             forBarMetrics:UIBarMetricsDefault];
//        [[UIBarButtonItem appearance] setBackButtonBackgroundImage:[[UIImage imageNamed:@"backArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//    }
    self.navigationController.navigationBar.alpha = 1.0;
}

//#warning 该方法为改变所有控件的字体，但现在不知道是否管用
//-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
//{
//    if ([view isKindOfClass:[UILabel class]])
//    {
//        UILabel *lbl = (UILabel *)view;
//        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
//    }
//    
//    if (isSubViews)
//    {
//        for (UIView *sview in view.subviews)
//        {
//            [self setFontFamily:fontFamily forView:sview andSubViews:YES];
//        }
//    }
//}

-(void)backItem{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    NSString *className = NSStringFromClass([self class]);
    
//    BaiduMobStat* statTracker = [BaiduMobStat defaultStat];
//    [statTracker eventEnd:@"changectl" eventLabel:[StringUtils isEmptyString:self.title]?className:self.title];
//    [statTracker logEventWithDurationTime:@"changectl" eventLabel:className durationTime:1.0];
//    [statTracker pageviewEndWithName:className];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
//    NSString *className = NSStringFromClass([self class]);
    
//    BaiduMobStat* statTracker = [BaiduMobStat defaultStat];
//    [statTracker eventStart:@"changectl" eventLabel:[StringUtils isEmptyString:self.title]?className:self.title];
//    [statTracker logEventWithDurationTime:@"changectl" eventLabel:className durationTime:1.0];
//    [statTracker pageviewStartWithName:className];
}

//#warning 这里是设置返回按钮的注释
//    self.navigationItem.backBarButtonItem.image = [UIImage imageNamed:@"icon-back.png"];
//        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon-back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backItem)];
//        ;
//        [[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"All_01"] forState:UIControlStateNormal style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault];

//        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"All_01"] forBarMetrics:UIBarMetricsDefault];
//        self.navigationItem.backBarButtonItem = nil;
//        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
//        [button addTarget:self action:@selector(backItem) forControlEvents:UIControlEventTouchUpInside];
//        [button setBackgroundImage:[UIImage imageNamed:@"icon-back.png"] forState:UIControlStateNormal];
//        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-back.png"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(backItem)];
//
//        self.navigationItem.leftBarButtonItem = item;

//    self.navigationItem.backBarButtonItem.image = [UIImage imageNamed:@"icon-back.png"];

//获取根控制器
+ (UIViewController *)currentWidowsViewController
{
    UIViewController *result = nil;
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow *temp in windows) {
            if (temp.windowLevel == UIWindowLevelNormal) {
                window = temp;
                break;
            }
        }
    }
    result = window.rootViewController;
    while (1) {
        if (result.presentedViewController) {
            result = result.presentedViewController;
        }else if ([result isKindOfClass:[UITabBarController class]]) {
            result = [(UITabBarController *)result selectedViewController];
        }else if ([result isKindOfClass:[UINavigationController class]]) {
            result = [(UINavigationController *)result visibleViewController];
        }else{
            break;
        }
    }

    return result;
}
@end
